#include "files.h"
#include "addmask.h"
#include "qmessagebox.h"
#include "qsettings.h"


Files::Files(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
    QSettings settings("HKEY_CURRENT_USER\\Software\\Files\\",
                        QSettings::NativeFormat);
    ui.OnOff->setChecked(settings.value("on-off").toBool());
    QString list = settings.value("list").toString();
    QString temp;
    for(int i = 0; i < list.count(); i++)
    {
        temp += list[i];
        if(list[i] == ';')
        {
            temp[temp.count() - 1] = '\0';
            ui.listWidget->addItem(new QListWidgetItem(temp));
            temp.clear();
        }

    }
	ui.Find->setFocus();

}

Files::~Files()
{
}

void Files::on_ButtonAddMask_clicked()
{
    AddMask *w = new AddMask(this);
    w->show();
}

void Files::on_ButtonAddFile_clicked()
{
        const QString filePath = QFileDialog::getOpenFileName(this,
            tr("Открыть файл"),"",tr("Все файлы (*.*)"));
         if (!filePath.isEmpty())
         ui.listWidget->addItem(new QListWidgetItem(filePath));
}

void Files::on_ButtonDelete_clicked()
{
    delete ui.listWidget->item(ui.listWidget->currentRow());
}

const void Files::add_item(const QString s)
{
    //ui.listWidget->addItem(new QListWidgetItem(s));
	QMessageBox::information(this, "", s);
}

void Files::on_Find_textChanged(const QString &arg1)
{
	for(int i = 0; i < ui.listWidget->count(); i++)
		ui.listWidget->setItemHidden(ui.listWidget->item(i), true);

    for(int i = 0; i < ui.listWidget->count(); i++)
    {
		if(!arg1.isEmpty())
			{
			for(int j = 0; j < ui.listWidget->item(i)->text().count(); j++)
				{
					if(ui.listWidget->item(i)->text().at(j) == arg1.at(arg1.count() - 1))
						ui.listWidget->setItemHidden(ui.listWidget->item(i), false);
				}
			}
			else
			{
				for(int i = 0; i < ui.listWidget->count(); i++)
					ui.listWidget->setItemHidden(ui.listWidget->item(i), false);
			}
	}	
      
}

void Files::on_ButtonSave_clicked()
{
    QSettings settings("HKEY_CURRENT_USER\\Software\\Files\\",
                        QSettings::NativeFormat);
    settings.setValue("on-off", ui.OnOff->isChecked());
    QString list;
    for(int i = 0; i < ui.listWidget->count(); i++)
    {
        list += ui.listWidget->item(i)->text() + ";";
    }
    settings.setValue("list", list);


}

void Files::on_ButtonAddPath_clicked()
{
	const QString filePath = QFileDialog::getExistingDirectory(this,
		tr("Открыть папку"), "");
     if (!filePath.isEmpty())
     ui.listWidget->addItem(new QListWidgetItem(filePath));
}
